extends Node2D

#方向信号
signal force_changed(direction)

# 轮盘位置和尺寸
var wheel_radius=0 #轮盘半径
var indicator_radius=0 # 指示点半径
var wheel_center: Vector2 = Vector2.ZERO #盘中心
var indicator_center: Vector2 = Vector2.ZERO #指示点中心

# 轮盘状态
var is_wheel_active = false #是否激活


func _ready():
	var screen_size = get_viewport().size
	wheel_radius = max(screen_size.x, screen_size.y)/12
	indicator_radius = wheel_radius/3


func _input(event):
	if event is InputEventScreenTouch or event is InputEventMouseButton:
		if event.pressed:
			# 当屏幕被点击时，激活轮盘
			is_wheel_active = true
			wheel_center = event.position #确定轮盘中心
			indicator_center = event.position #初始化指示点位置
		else:
			# 当手指离开屏幕时，隐藏轮盘
			is_wheel_active = false 
			emit_signal("force_changed",Vector2.ZERO )
	elif event is InputEventScreenDrag or event is InputEventMouseMotion:
		# 确定指示点中心
		if (event.position-wheel_center).length() > wheel_radius:
			var direction = (event.position-wheel_center).normalized() 
			indicator_center = direction*wheel_radius+wheel_center
		else:
			indicator_center = event.position

func _process(delta):
	if is_wheel_active:
		#发送方向信号
		var force = (indicator_center - wheel_center)/wheel_radius #归一化的力
		emit_signal("force_changed",force)
	queue_redraw()

#绘制轮盘
func _draw():
	if is_wheel_active:
		draw_circle(wheel_center,wheel_radius,Color.AQUA)
		draw_circle(indicator_center,indicator_radius,Color.CHARTREUSE)


#比较丝滑的剑属性
# mass = 0.01
# damp = 5
